﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Transactions;

public partial class WebForm_Auth_OrgAuth : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            plQuery.DataBind();
            gv.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var db = new DB();

        using (TransactionScope scope = new TransactionScope())
        {
            db.ExecuteNonQuery(@"INSERT RoleOrgMap (RoleID, OrgID)
                                    SELECT @RoleID, @OrgID"
                , new DBParameter("OrgID", ddlOrgID.Text), new DBParameter("RoleID", txtRoleID.Text));

            scope.Complete();
        }
        plQuery.DataBind();
        gv.DataBind();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        gv.DataBind();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        gv.DataBind();
    }

    protected void gv_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "MyDelete":
                var row = (e.CommandSource as Control).NamingContainer as GridViewRow;
                var db = new DB();

                using (TransactionScope scope = new TransactionScope())
                {
                    db.ExecuteNonQuery("DELETE RoleOrgMap WHERE OrgID = @OrgID AND RoleID = @RoleID "
                        , new DBParameter("OrgID", gv.DataKeys[row.RowIndex]["OrgID"])
                        , new DBParameter("RoleID", gv.DataKeys[row.RowIndex]["RoleID"]));

                    scope.Complete();
                }

                plQuery.DataBind();
                gv.DataBind();

                break;

            default:
                break;
        }
    }
}